package com.ysd.twocrm.quartz;

import com.ysd.twocrm.entity.UserRoles;
import com.ysd.twocrm.mapper.UserRolesMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Component
@EnableScheduling//开启定时任务
public class userRolesQuartz {
    @Autowired
    private UserRolesMapper userRolesMapper;

    /*
     * @Description 每月最后一天执行
     * 先查询出当前月份下没有完成过订单的咨询师
     * 然后将这些咨询师的权重值都-10
     * 当然如果每次执行的时候发现他是负数的话就给他修改封0
     * @param
     * @return void
     * @author liusupeng
     * @date 2020/5/19 8:41
     */
@Scheduled(cron = "0 0 0 31 * *")//秒分时日月周
    public void findAllBylogs(){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM");
        List<UserRoles> allByStudentLogs = userRolesMapper.findAllByStudentLogs(sdf.format(new Date()));
        for(int i=0;i<allByStudentLogs.size();i++){
            if(allByStudentLogs.get(i).getUserRolesWeight()>0){
                //当前权重值还没成负数
                Integer weight=allByStudentLogs.get(i).getUserRolesWeight()-10;
                if(weight<0){
                    weight=0;
                }
                userRolesMapper.exitWeight(allByStudentLogs.get(i).getUserRolesId(),weight);
            }else{
                //当前权重值已变成负数，要修改成0
                userRolesMapper.exitWeight(allByStudentLogs.get(i).getUserRolesId(),0);
            }
        }
    }
}
